<?php
/**
 * Created by PhpStorm.
 * User: wodrow
 * Date: 18-2-1
 * Time: 下午1:40
 */

namespace api\modules\chedai\controllers;


use common\components\tools\Tools;
use common\models\AndroidAppVersions;
use common\models\credit_app\MobileSaveVideo;
use wodrow\yii\rest\Controller;
use yii\base\ErrorException;

class OthersController extends Controller
{
    /**
     * 手机端保存客户视频地址
     * @desc get/post
     * @param string $idno 身份证
     * @param string $url 上传后地址
     * @param string $path 手机本地地址
     * @return array
     * @throws ErrorException
     */
    public function actionSaveVideoData($idno, $url = null, $path = null)
    {
        if ($idno==''){
            throw new ErrorException("身份证不能空!");
        }
        $m = MobileSaveVideo::findOne(['idno'=>$idno, 'url' => $url, 'path'=>$path]);
        if ($m){
            return $m->toArray();
        }else{
            $m = new MobileSaveVideo();
            $m->idno = $idno;
            if ($url)$m->url=$url;
            if ($path)$m->path = $path;
        }
        $m->created_at = time();
        if($m->save()){
            return $m->toArray();
        }else{
            throw new ErrorException("保存异常");
        }
    }

    /**
     * 手机端根据id更新客户视频地址数据
     * @desc get/post
     * @param int $id
     * @param string $url
     * @param string $path
     * @return array
     * @throws ErrorException
     */
    public function actionUpdateSaveVideoById($id, $url=null, $path=null)
    {
        $m = MobileSaveVideo::findOne(['id'=>$id]);
        if (!$m){
            throw new ErrorException("没有找到视频!");
        }
        if ($url)$m->url=$url;
        if ($path)$m->path=$path;
        $m->save();
        return $m->toArray();
    }

    /**
     * 手机端根据身份证号获取所有视频地址
     * @desc get/post
     * @param string $idno 身份证号
     * @param string $order_id 订单号
     * @return array videos
     */
    public function actionGetAllSaveVideoByIdno($idno = null, $order_id = null)
    {
        $videos = [];
        if ($order_id){
            $videos = MobileSaveVideo::find()->where(['order_id'=>$order_id])->asArray()->all();
            return $videos;
        }
        if ($idno)$videos = MobileSaveVideo::find()->where(['idno'=>$idno])->asArray()->all();
        return $videos;
    }

    /**
     * 用户是否在黑名单
     * @desc get/post
     * @param integer $uid 用户id
     * @return array versions 版本列表
     * @return bool can_do 是否可以操作
     */
    public function actionVersionList($uid = null)
    {
        $lv = AndroidAppVersions::getInUse();
        $can_do = 1;
        $bul = $lv->black_user_list;
        $cant_do_ids = $bul?Tools::formatArrStrToArr($bul):[];
        if (in_array($uid, $cant_do_ids)){
            $can_do = 0;
        }
        $wl = $lv->white_version_list;
        $y = $wl?Tools::formatArrStrToArr($wl):[];
        $r = [
            'versions'=>$y,
            'can_do' => $can_do,
            'version_data' => $lv->toArray(),
        ];
        return $r;
    }

    /**
     * 获取APP最新版本
     */
    public function actionLastVersionGet()
    {
        $lv = AndroidAppVersions::getInUse();
        $o = [
            'v_number' => $lv->v_number,
            'v_download' => $lv->app_download_url?$lv->app_download_url:\Yii::$app->params['api_domain']."/chedai/others/last-version-download",
            'v_title' => $lv->v_title,
            'face_mode' => $lv->face_mode,
            'update_db' => $lv->update_db,
            'update_db_path' => $lv->update_db_path,
            'v_content' => $lv->v_content,
            'notify' => $lv->notify,
        ];
        $o_d_uids = $lv->other_download_u_ids;
        $o_d_uids = $o_d_uids?Tools::formatArrStrToArr($o_d_uids):[];
        if (!\Yii::$app->user->isGuest){
            $u_id = \Yii::$app->user->id;
            if (in_array($u_id, $o_d_uids)){
                $lv = AndroidAppVersions::getOtherUse();
                $o = [
                    'v_number' => $lv->v_number,
                    'v_download' => $lv->app_download_url?$lv->app_download_url:\Yii::$app->params['api_domain']."/chedai/others/other-version-download",
                    'v_title' => $lv->v_title,
                    'face_mode' => $lv->face_mode,
                    'update_db' => $lv->update_db,
                    'update_db_path' => $lv->update_db_path,
                    'v_content' => $lv->v_content,
                    'notify' => $lv->notify,
                ];
            }
        }
        return $o;
    }

    /**
     * 下载最新版本APP
     */
    public function actionLastVersionDownload()
    {
        $filename = \Yii::getAlias('@data').DIRECTORY_SEPARATOR."attsort".DIRECTORY_SEPARATOR."app-carloan-release.apk";
        if(!file_exists($filename)){
            exit("无法找到文件"); //即使创建，仍有可能失败。。。。
        }
        header("Cache-Control: public");
        header("Content-Description: File Transfer");
        header('Content-disposition: attachment; filename='.basename($filename)); //文件名
        header("Content-Type: application/apk"); //apk格式的
        header("Content-Transfer-Encoding: binary"); //告诉浏览器，这是二进制文件
        header('Content-Length: '. filesize($filename)); //告诉浏览器，文件大小
//        @readfile($filename);
        $filename = \Yii::getAlias('@data').DIRECTORY_SEPARATOR."attsort".DIRECTORY_SEPARATOR."app-carloan-release.apk";
        Tools::download2($filename);
    }

    /**
     * 下载其他版本APP
     */
    public function actionOtherVersionDownload()
    {
        $filename = \Yii::getAlias('@data').DIRECTORY_SEPARATOR."attsort".DIRECTORY_SEPARATOR."app-carloan-other.apk";
        if(!file_exists($filename)){
            exit("无法找到文件"); //即使创建，仍有可能失败。。。。
        }
        header("Cache-Control: public");
        header("Content-Description: File Transfer");
        header('Content-disposition: attachment; filename='.basename($filename)); //文件名
        header("Content-Type: application/apk"); //apk格式的
        header("Content-Transfer-Encoding: binary"); //告诉浏览器，这是二进制文件
        header('Content-Length: '. filesize($filename)); //告诉浏览器，文件大小
        @readfile($filename);
    }
}